package com.itsu.job.rpc.request;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import com.itsu.job.context.JobHelper;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author Jerry.Su
 * @Date 2022/1/28 14:22
 */
@Slf4j
public class StopJobRequestHandler extends JobRpcRequestHandler {

    final JobHelper jobHelper;

    public StopJobRequestHandler(JobHelper jobHelper) {
        this.jobHelper = jobHelper;
    }

    @Override
    public Map<String, Object> apply(String uri, String requestData) {
        MapBuilder<String, Object> builder = MapUtil.builder();
        try {
            Map<String, Object> params = parseParams(requestData);
            jobHelper.stop((String) params.get("jobId"), (Integer) params.get("type"));
            builder.put("code", 0);
        } catch (Exception e) {
            log.error("stop job error", e);
            builder.put("code", -1);
            builder.put("msg", e.getMessage());
        }
        return builder.build();
    }


}
